<template>
  <div>
    {{count}}
    <div>
      <button @click="addCount()">+1</button>
      <button @click="minusCount()">-1</button>
    </div>
    <div>
      <button @click="addCountAsync()">+1 async</button>
      <button @click="minusCountAsync()">-1 async</button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  methods: {
    addCount: function() {
      this.$store.commit('addCount', 1);
    },
    minusCount: function() {
      this.$store.commit('minusCount', 1);
    },
    addCountAsync: function() {
      this.$store.dispatch('addCountAsync', 1);
    },
    minusCountAsync: function() {
      this.$store.dispatch('minusCountAsync', 1);
    },
  },
  computed: {
    count: function() {
      return this.$store.state.count;
    }
  },
  watch: {
    'count': {
      handler: function(newVal, oldVal) {
        console.log('打印count值', newVal, oldVal);
      },
      immediate: true
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
